package com.csce.domain.entity.score.socket;

import com.doudou.core.container.ListUtil;
import com.doudou.core.str.StrUtil;
import lombok.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author zzxjs
 * @since 2023/11/15
 * 两端互联实体类
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
@ToString
public class Connect implements Serializable {

    private Long examId;
    private String timeId;
    private String placeId;
    private Integer index;
    /**
     * 唯一标识
     */
    private String unique;

    /**
     * 屏幕端连接
     */
    private String showSession;

    /**
     * 控制端连接
     */
    private CtrlSession ctrlSession = new CtrlSession();

    /**
     * 门口展示屏连接
     */
    private String doorSession;

    public Connect(Long examId, String timeId, String placeId, Integer index) {
        this.examId = examId;
        this.timeId = timeId;
        this.placeId = placeId;
        this.index = index;
        this.unique = createUnique(examId + "", timeId, placeId, index);
    }

    public void clearCtrl() {
        this.ctrlSession.clear();
    }

    public boolean showEquals(String sessionId) {
        return this.hasShow() && StrUtil.equals(this.showSession, sessionId);
    }

    public boolean hasShow() {
        return StrUtil.isNotBlank(this.showSession);
    }

    public boolean notHasShow() {
        return !hasShow();
    }

    public void clearShow() {
        this.showSession = null;
    }

    public boolean doorEquals(String sessionId) {
        return StrUtil.equals(this.doorSession, sessionId);
    }

    public boolean hasDoor() {
        return StrUtil.isNotBlank(this.doorSession);
    }

    public boolean notHasDoor() {
        return !hasDoor();
    }

    public void clearDoor() {
        this.doorSession = null;
    }

    public void clear(){
        clearDoor();
        clearCtrl();
        clearShow();
    }


    public static String createUnique(String examId, String timeId, String placeId, Integer index) {
        return StrUtil.joinSep("-", examId, timeId, placeId, index);
    }

    public boolean hasCtrl(){
        return this.ctrlSession.hasScene() || this.ctrlSession.hasCenter() || this.ctrlSession.hasSsh();
    }


    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Builder
    @ToString
    public static class CtrlSession{
        //现场
        private String sceneSession;
        //中控
        private String centerSession;
        //远程
        private String sshSession;

        public boolean sceneEquals(String sessionId) {
            return this.hasScene() && StrUtil.equals(this.sceneSession, sessionId);
        }

        public boolean centerEquals(String sessionId) {
            return this.hasCenter() && StrUtil.equals(this.centerSession, sessionId);
        }

        public boolean sshEquals(String sessionId) {
            return this.hasSsh() && StrUtil.equals(this.sshSession, sessionId);
        }

        public boolean hasScene(){
            return StrUtil.isNotBlank(this.sceneSession);
        }

        public boolean hasCenter(){
            return StrUtil.isNotBlank(this.centerSession);
        }

        public boolean hasSsh(){
            return StrUtil.isNotBlank(this.sshSession);
        }

        public boolean notHasScene(){
            return !this.hasScene();
        }

        public boolean notHasCenter(){
            return !hasCenter();
        }

        public boolean notHasSsh(){
            return !hasSsh();
        }


        public void clearScene(){
            this.sceneSession = null;
        }

        public void clearCenter(){
            this.centerSession = null;
        }

        public void clearSsh(){
            this.sshSession = null;
        }

        public void clear(){
            this.sceneSession = null;
            this.centerSession = null;
            this.sshSession = null;
        }
    }
}
